import md5 from '../../common/md5.js';
export default {
	created() {
		document.title = 'maybe'
	},
	data() {
		return {
			username: '',
			password: '',
			clientip: '',
			nasip: '183.56.17.19',
			mac: '',
			timestamp: '',
			authenticator: '',
			challenge: '',

			selectId: '1',
			select1: true,
			select2: false,
			select3: false
		}
	},
	mounted: function() {
		//在页面加载时，获取缓存数据
		this.username = uni.getStorageSync("username1");
		this.password = uni.getStorageSync("password1");
		this.clientip = uni.getStorageSync("clientip1");
		this.mac = uni.getStorageSync("mac1");
	},
	methods: {
		selectOpen(e) {
			this.selectId = e;

			let username = "username" + this.selectId;
			let password = "password" + this.selectId;
			let clientip = "clientip" + this.selectId;
			let mac = "mac" + this.selectId;

			switch (e) {
				case '1':
					this.select1 = true;
					this.select2 = false;
					this.select3 = false;
					break;
				case '2':
					this.select1 = false;
					this.select2 = true;
					this.select3 = false;
					break;
				case '3':
					this.select1 = false;
					this.select2 = false;
					this.select3 = true;
					break;
				default:
					this.select1 = true;
					this.select2 = false;
					this.select3 = false;
					break;
			}

			this.username = uni.getStorageSync(username);
			this.password = uni.getStorageSync(password);
			this.clientip = uni.getStorageSync(clientip);
			this.mac = uni.getStorageSync(mac);
		},
		teach() {
			uni.showModal({
				title: '使用教程',
				showCancel: false,
				confirmColor: '#1890ff',
				content: '将要联网的设备（电脑、路由器等）接上校园网网线，可查看该网络设备的IPv4地址与MAC（物理地址）。\n\n电脑端连接网线后在CMD中输入"ipconfig /all"命令即可查看设备信息。\n\n路由器端连接网线后可在路由器管理界面查看设备信息。',
				success: function(res) {

				}
			});

		},
		openUrl() {
			plus.runtime.openURL('https://gitee.com/wx_1cd83889b0/maybe_xyw', function(
				res) {
			});
		},
		login() {

			if (this.username == "" || this.password == "" || this.clientip == "" || this.mac == "") {
				uni.showToast({
					title: '请完善登录信息',
					icon: 'none',
					duration: 2000
				});
				return;
			}

			//数据缓存
			uni.setStorage({
				key: 'username' + this.selectId,
				data: this.username,
				success: function() {

				}
			});
			uni.setStorage({
				key: 'password' + this.selectId,
				data: this.password,
				success: function() {

				}
			});
			uni.setStorage({
				key: 'clientip' + this.selectId,
				data: this.clientip,
				success: function() {

				}
			});
			uni.setStorage({
				key: 'mac' + this.selectId,
				data: this.mac,
				success: function() {

				}
			});

			this.timestamp = parseInt(new Date().getTime()) + '';
			this.authenticator = md5(this.clientip + this.nasip + this.mac + this.timestamp + 'Eshore!@#').toString()
				.toLocaleUpperCase(); //md5加密，并全部转成大写
			console.log(this.authenticator)
			let data1 = {
				"username": this.username,
				"clientip": this.clientip,
				"nasip": this.nasip,
				"mac": this.mac,
				"timestamp": this.timestamp,
				"authenticator": this.authenticator
			}

			this.$request('/client/challenge', data1).then(res1 => {

				this.timestamp = parseInt(new Date().getTime()) + '';
				this.authenticator = md5(this.clientip + this.nasip + this.mac + this.timestamp + res1
					.challenge + 'Eshore!@#').toString().toLocaleUpperCase(); //md5加密，并全部转成大写

				let data2 = {
					"username": this.username,
					"password": this.password,
					"clientip": this.clientip,
					"nasip": this.nasip,
					"mac": this.mac,
					"timestamp": this.timestamp,
					"authenticator": this.authenticator
				}

				this.$request('/client/login', data2).then(res2 => {
					if (res2.rescode == 0) {
						uni.showToast({
							title: '登陆成功',
							icon: 'none',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: '登陆失败',
							icon: 'none',
							duration: 2000
						});
					}
				})
			})
		},
	}
}
